package com.ax.associator.shopassistant.service.Impl;

import com.ax.associator.shopassistant.dao.BasePersonnelMapper;
import com.ax.associator.shopassistant.dao.SysUserMapper;
import com.ax.associator.shopassistant.domain.*;
import com.ax.associator.shopassistant.service.BasePersonnelService;
import com.ax.associator.store.common.utils.BirthUtil;
import com.ax.associator.store.common.utils.ErrorUtil;
import com.ax.associator.store.common.utils.PageUtils;
import com.ax.associator.store.common.utils.Query;
import com.ax.associator.store.common.utils.base.MD5;
import com.ax.associator.store.dao.AttachmentMapper;
import com.ax.associator.store.dao.BusiAttachRelationMapper;
import com.ax.associator.store.domain.Attachment;
import com.ax.associator.store.domain.BusiAttachRelation;
import com.ax.base.system.domain.SysUserDO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Map;

/**
 * @Author Lg
 * @Date 2018/8/31 17:05
 * @Content
 */
@Service("BasePersonnelService")
public class BasePersonnelServiceImpl implements BasePersonnelService{

    @Autowired
    AttachmentMapper attachmentMapper;

    @Autowired
    BasePersonnelMapper basePersonnelMapper;

    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    BusiAttachRelationMapper busiAttachRelationMapper;

    /**
     * 新增店员相关综合信息
     * @param basePersonnel
     * @param headImgUrl
     * @return
     */
    @Override
    @Transactional
    public Integer insertInformation(BasePersonnel basePersonnel) throws Exception {
    //    Assert.notNull(headImgUrl,String.format(ErrorUtil.PARAMETER_NULL,"headImgUrl"));
        //保存头像
        Attachment attachment = new Attachment();
        attachment.setAttachmentUrl(basePersonnel.getHeadImgUrl());
        attachmentMapper.insertSelective(attachment);
        //验证身份证和号码不重复
        int result1 = basePersonnelMapper.queryBasePersonnelCount(basePersonnel.getPersonId(),null);
        Assert.state(result1==0,"已存在相同身份证店员");
        int result2 = basePersonnelMapper.queryBasePersonnelCount(null,basePersonnel.getPhone());
        Assert.state(result2==0,"已存在相同号码店员");
        //新增店员
        basePersonnel.setDrugstoreId(1l);
        basePersonnel.setDrugstoreName("红梅");
        basePersonnelMapper.insertSelective(basePersonnel);
        //新增用户
        SysUserDO SysUserDO = new SysUserDO();
        SysUserDO.setUserTypeCode("03");
        SysUserDO.setUserTypeName("药店人员");
        String a = basePersonnel.getPersonnelId().toString();
        SysUserDO.setUserCode(Integer.parseInt(a));
        SysUserDO.setUsername(basePersonnel.getPersonnelName());
        SysUserDO.setFlag(1);
        SysUserDO.setRealTimeState(0);
        sysUserMapper.insertSysUser(SysUserDO);
        //新增店员头像关联
        BusiAttachRelation busiAttachRelation2 = new BusiAttachRelation();
        busiAttachRelation2.setBusinessSeq(String.valueOf(SysUserDO.getUserId()));
        busiAttachRelation2.setAttachmentId(attachment.getAttachmentId());
        busiAttachRelation2.setBusinessType("BusiType");
        busiAttachRelation2.setBusinessCode("14");
        busiAttachRelationMapper.insertBusiAttachRelation(busiAttachRelation2);
        
        
        return 1;
    }

    @Override
    public Integer updateFlag(Integer flag, String[] personnelId) {
        return basePersonnelMapper.updateFlag(flag,personnelId);
    }

    @Override
    public Integer deleteBasePersonnel(Long personnelId) {
        return basePersonnelMapper.deleteByPrimaryKey(personnelId);
    }

    @Override
    public PageUtils queryBasePersonnel(Map<String,Object> basePersonnel) {
        Query query = new Query(basePersonnel);
        List<BasePersonnel> basePersonnelList = basePersonnelMapper.queryBasePersonnel(query);
        int total = basePersonnelMapper.queryCount(query);
        for(BasePersonnel li:basePersonnelList){
        	Integer age= BirthUtil.getAgeByBirth(li.getBirth());
        	li.setAge(age);
        }
        PageUtils pageUtil = new PageUtils(basePersonnelList,total);
        return pageUtil;
    }

    @Override
    public List<BasePerseonnelDto> queryDto(Long drugstoreId) {
        return basePersonnelMapper.queryDto(drugstoreId);
    }

    @Override
    public List<MenuDto> queryMenuDto() {
        return basePersonnelMapper.queryMenuDto();
    }

    @Override
    public Integer insertPersonnelMenu(Map<String, Object> map) {
        //新增用户
        SysUserDO sysUserDO = new SysUserDO();
        sysUserDO.setFlag(1);
        sysUserDO.setPassword(MD5.md5(map.get("password").toString()));
        sysUserDO.setUsername(map.get("phone").toString());
        //todo session取用户id session取药店信息
//      sysUserDO.setUserIdCreate();
        sysUserDO.setStatus(1);
        sysUserDO.setName(map.get("personnelName").toString());
        sysUserMapper.insertSysUser(sysUserDO);
        //新增用户菜单
        Long roleId = sysUserDO.getUserId();
        String[] menuIds = map.get("menuIds").toString().split(",");
        basePersonnelMapper.insertMenu(roleId,menuIds);
        Long personnelId = Long.valueOf(map.get("personnelId").toString());
        //查询店员关联头像id
        Integer attachmentId = basePersonnelMapper.queryPerseonnelAttId(personnelId);
        //新增附件关联（）
        BusiAttachRelation busiAttachRelation1 = new BusiAttachRelation();
        busiAttachRelation1.setBusinessCode(String.valueOf(sysUserDO.getUserId()));
        busiAttachRelation1.setAttachmentId(Long.valueOf(attachmentId));
        busiAttachRelationMapper.insertBusiAttachRelation(busiAttachRelation1);
        return 1;
    }

    @Override
    public List<MenuRoleDto> queryMenuRole(Long drugstoreId, String drugstoreName) {
        List<MenuRoleDto> menuRoleDtos = basePersonnelMapper.queryMenuRole(drugstoreId,drugstoreName);
        return menuRoleDtos;
    }

    @Override
    public Integer updateMenuRole(Map<String, Object> map) {
        return null;
    }


}
